<template>
  <t-dialog
    :header="header"
    :visible.sync="visible"
    :onClose="closeDialog"
    :closeOnOverlayClick="false"
    @confirm="onSubmit"
    :confirmBtn="{
        content: loading?'提交中...':'确认',
        theme: 'primary',
        loading: loading,
      }"
    width="800px"
    top="5%"
  >
    <t-form ref="form" :colon="true"  :data="formData" labelWidth="100px" :rules="rules">
      <t-form-item label="岗位名称" name="postName">
        <t-input clearable v-model="formData.postName" :maxlength="30" placeholder="请输入岗位名称"></t-input>
      </t-form-item>
      <t-form-item label="岗位编码" name="postCode">
        <t-input clearable v-model="formData.postCode" :maxlength="30" placeholder="请输入岗位编码"></t-input>
      </t-form-item>
      <t-form-item label="岗位顺序" name="postSort">
        <t-input-number clearable v-model="formData.postSort" :min="0" placeholder="请输入"></t-input-number>
      </t-form-item>
      <t-form-item label="岗位状态" name="status">
        <t-radio-group v-model="formData.status" :options="dict.type.sys_normal_disable">
          <!--          <t-radio value="1"> 正常</t-radio>-->
          <!--          <t-radio value="2"> 停用</t-radio>-->
        </t-radio-group>
      </t-form-item>
      <t-form-item label="备注" name="remark">
        <t-textarea
          v-model="formData.remark"
          placeholder="请输入备注"
        />
      </t-form-item>
    </t-form>
  </t-dialog>
</template>

<script>
export default {
  name: "add-edit-post",
  dicts: ["sys_normal_disable"],
  props:["postId","header"],
  data() {
    return {
      visible: true,
      loading: false,
      formData:{
        postName: '',
        postCode: '',
        postSort:0,
        status: '0',
        remark: '',
      },
      rules:{
        postName: [
          {
            required: true,
            message: "岗位名称不能为空",
          },
        ],
        postCode: [
          {
            required: true,
            message: "岗位编码不能为空",
          },
        ],
      }
    };
  },
  watch: {
    postId: {
      immediate: true,
      handler(val) {
        if (val) {
          this.getObj();
        }
      }
    }
  },
  methods: {
    getObj(){
      this.$api.system.post.get(this.postId).then(
        (res) => {
          this.formData = res.data;
        }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    closeDialog() {
      this.$emit("update:visible", false);
    },
    onSubmit() {
      this.$refs.form.validate().then((validateResult) => {
        if (validateResult === true) {
          this.loading = true;
          if (this.postId !== 0) {
            this.$api.system.post.edit(this.formData).then((response) => {
              this.$message.success("修改成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          } else {
            this.$api.system.post.add(this.formData).then((response) => {
              this.$message.success("新增成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          }
        }
      });
    }
  }
};
</script>

<style scoped>

</style>
